home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware in MacFormat / brailler0.5b / brlr ƒ / Shell ƒ / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  7.8 KB  |  329 lines  |  [TEXT/MMCC]

  1. #include "help.h"
  2. #include "environment.h"
  3. #include "popup.h"
  4. #include "util.h"
  5. #include "buttons.h"
  6. #include "text twiddling.h"
  7. #include "program globals.h"
  8. #include "window layer.h"
  9. #include "graphics.h"
  10.  
  11. #define DEAD_SPACE_TOP        10
  12. #define DEAD_SPACE_LEFT        10
  13. #define DEAD_SPACE_BOTTOM    5
  14. #define DEAD_SPACE_RIGHT    10
  15. #define    TEXT_RECT_WIDTH        405
  16. #define    TEXT_RECT_HEIGHT    250
  17. #define    BUTTON_WIDTH        80
  18. #define    BUTTON_HEIGHT        17
  19. #define BUTTON_GAP_H        15
  20. #define BUTTON_GAP_V        5
  21.  
  22. #define MAX_MAIN_TOPICS        5
  23. #define    MAX_SUB_TOPICS        6
  24.  
  25. #define MAIN_TOPIC_ID        600
  26. #define POPUP_MENU_ID        100
  27.  
  28. short            gMainTopicShowing;        /* saved in prefs file */
  29. short            gSubTopicShowing;        /* saved in prefs file */
  30.  
  31. static    short            gNumMainTopics;
  32. static    short            gNumSubTopics[MAX_MAIN_TOPICS];
  33. static    Str31            gMainTopicTitle[MAX_MAIN_TOPICS];
  34. static    Rect            gMainTopicRect[MAX_MAIN_TOPICS];
  35. static    Str31            gSubTopicTitle[MAX_MAIN_TOPICS][MAX_SUB_TOPICS];
  36. static    short            gSubTopicID[MAX_MAIN_TOPICS][MAX_SUB_TOPICS];
  37. static    Rect            gTextRect;
  38. static    Handle            gTheText;
  39. static    Handle            gTheStyle;
  40.  
  41. static    Boolean            gSetupDone=FALSE;
  42.  
  43. /*-----------------------------------------------------------------------------------*/
  44. /* internal stuff for help.c                                                         */
  45.  
  46. static    short ParseRawTitle(Str255 theTitle);
  47. static    void GoToPage(WindowPtr theWindow, short mainTopic, short subTopic,
  48.                 Boolean updateNow);
  49. static    void GetTextResources(short mainTopic, short subTopic);
  50. static    void DisposeTextResources(void);
  51.  
  52. void SetupTheHelpWindow(WindowPtr theWindow)
  53. {
  54.     short            i,j;
  55.     unsigned char    *titleStr="\pHelp";
  56.     Handle            temp;
  57.     short            strID;
  58.     Point            thePoint;
  59.     
  60.     SetWindowMaxDepth(theWindow, 8);
  61.     SetWindowDepth(theWindow, 8);
  62.     SetWindowWidth(theWindow, DEAD_SPACE_LEFT+TEXT_RECT_WIDTH+DEAD_SPACE_RIGHT);
  63.     SetWindowHeight(theWindow, BUTTON_GAP_V+DEAD_SPACE_TOP+BUTTON_HEIGHT+TEXT_RECT_HEIGHT+
  64.         DEAD_SPACE_BOTTOM);
  65.     SetWindowType(theWindow, noGrowDocProc);
  66.     SetWindowHasCloseBox(theWindow, TRUE);
  67.     thePoint.v=50;
  68.     thePoint.h=6;
  69.     SetWindowTopLeft(theWindow, thePoint);
  70.     SetWindowIsFloat(theWindow, FALSE);
  71.     SetWindowTitle(theWindow, titleStr);
  72.     SetWindowAutoCenter(theWindow, FALSE);
  73.     
  74.     if (gSetupDone)
  75.         return;
  76.     
  77.     temp=GetResource('STR#', MAIN_TOPIC_ID);
  78.     gNumMainTopics=**((short**)temp);
  79.     ReleaseResource(temp);
  80.     for (i=0; i<gNumMainTopics; i++)
  81.     {
  82.         GetIndString(gMainTopicTitle[i], MAIN_TOPIC_ID, i+1);
  83.         strID=ParseRawTitle(gMainTopicTitle[i]);
  84.         
  85.         for (j=0; j<5; j++)
  86.             gMainTopicTitle[i][++gMainTopicTitle[i][0]]=' ';
  87.         
  88.         SetRect(    &gMainTopicRect[i],
  89.                     DEAD_SPACE_LEFT+(BUTTON_WIDTH+BUTTON_GAP_H)*i,
  90.                     DEAD_SPACE_TOP,
  91.                     DEAD_SPACE_LEFT+(BUTTON_WIDTH+BUTTON_GAP_H)*i+BUTTON_WIDTH,
  92.                     DEAD_SPACE_TOP+BUTTON_HEIGHT);
  93.         
  94.         temp=GetResource('STR#', strID);
  95.         gNumSubTopics[i]=**((short**)temp);
  96.         ReleaseResource(temp);
  97.         
  98.         for (j=0; j<gNumSubTopics[i]; j++)
  99.         {
  100.             GetIndString(gSubTopicTitle[i][j], strID, j+1);
  101.             gSubTopicID[i][j]=ParseRawTitle(gSubTopicTitle[i][j]);
  102.         }
  103.     }
  104.     
  105.     gTheText=0L;
  106.     gTheStyle=0L;
  107.     
  108.     SetRect(&gTextRect, DEAD_SPACE_LEFT+4, DEAD_SPACE_TOP+BUTTON_HEIGHT+BUTTON_GAP_V+4,
  109.         DEAD_SPACE_LEFT+TEXT_RECT_WIDTH-4,
  110.         DEAD_SPACE_TOP+BUTTON_HEIGHT+BUTTON_GAP_V+TEXT_RECT_HEIGHT-4);
  111.     
  112.     gSetupDone=TRUE;
  113. }
  114.  
  115. void OpenTheHelpWindow(WindowPtr theWindow)
  116. {
  117.     TEHandle        hTE;
  118.     Rect            destRect, viewRect;
  119.     
  120.     hTE=GetWindowTE(theWindow);
  121.     if (hTE==0L)
  122.     {
  123.         destRect=gTextRect;
  124.         viewRect=destRect;
  125.         hTE=TEStyleNew(&destRect, &viewRect);
  126.         SetWindowTE(theWindow, hTE);
  127.         TESetSelect(0, 0, hTE);
  128.         TEAutoView(TRUE, hTE);
  129.         TESelView(hTE);
  130.         GoToPage(theWindow, gMainTopicShowing, gSubTopicShowing, FALSE);
  131.     }
  132. }
  133.  
  134. void ShutDownTheHelpWindow(void)
  135. {
  136.     DisposeTextResources();
  137. }
  138.  
  139. void DisposeTheHelpWindow(WindowPtr theWindow)
  140. {
  141.     TEHandle        hTE;
  142.     
  143.     hTE=GetWindowTE(theWindow);
  144.     if (hTE!=0L)
  145.     {
  146.         TEDispose(hTE);
  147.         SetWindowTE(theWindow, 0L);
  148.     }
  149. }
  150.  
  151. void KeyPressedInHelpWindow(WindowPtr theWindow, unsigned char keyPressed)
  152. {
  153.     ObscureCursor();
  154.     
  155.     switch (keyPressed)
  156.     {
  157.         case 0x1d:                                        /* right arrow */
  158.             gSubTopicShowing++;
  159.             if (gSubTopicShowing>=gNumSubTopics[gMainTopicShowing])
  160.             {
  161.                 gSubTopicShowing=0;
  162.                 gMainTopicShowing++;
  163.                 if (gMainTopicShowing>=gNumMainTopics)
  164.                     gMainTopicShowing=0;
  165.             }
  166.             GoToPage(theWindow, gMainTopicShowing, gSubTopicShowing, TRUE);
  167.             break;
  168.         case 0x1c:                                        /* left arrow */
  169.             gSubTopicShowing--;
  170.             if (gSubTopicShowing<0)
  171.             {
  172.                 gMainTopicShowing--;
  173.                 if (gMainTopicShowing<0)
  174.                     gMainTopicShowing=gNumMainTopics-1;
  175.                 gSubTopicShowing=gNumSubTopics[gMainTopicShowing]-1;
  176.             }
  177.             GoToPage(theWindow, gMainTopicShowing, gSubTopicShowing, TRUE);
  178.             break;
  179.     }
  180. }
  181.  
  182. void MouseClickedInHelpWindow(WindowPtr theWindow, Point mouseLoc)
  183. {
  184.     short            i;
  185.     short            newMain, newSub;
  186.     MenuHandle        theMenu;
  187.     Rect            menuRect;
  188.     
  189.     newMain=-1;
  190.     
  191.     for (i=0; i<gNumMainTopics; i++)
  192.     {
  193.         if (PtInRect(mouseLoc, &gMainTopicRect[i]))
  194.         {
  195.             newMain=i;
  196.             i=gNumMainTopics;
  197.         }
  198.     }
  199.     
  200.     if (newMain!=-1)
  201.     {
  202.         Draw3DButton(&gMainTopicRect[newMain], gMainTopicTitle[newMain], 0L,
  203.             GetWindowDepth(theWindow), TRUE, TRUE);
  204.         
  205.         theMenu=NewMenu(POPUP_MENU_ID, "\p");
  206.         for (i=0; i<gNumSubTopics[newMain]; i++)
  207.         {
  208.             AppendMenu(theMenu, gSubTopicTitle[newMain][i]);
  209.             CheckItem(theMenu, i+1, ((newMain==gMainTopicShowing) && (i==gSubTopicShowing)));
  210.         }
  211.         
  212.         menuRect.top=gMainTopicRect[newMain].bottom-1;
  213.         menuRect.left=gMainTopicRect[newMain].left+1;
  214.         newSub=-1;
  215.         if (MouseInModelessPopUp(theMenu, &newSub, &menuRect, POPUP_MENU_ID))
  216.         {
  217.             GoToPage(theWindow, newMain, newSub-1, TRUE);
  218.         }
  219.         else
  220.         {
  221.             Draw3DButton(&gMainTopicRect[newMain], gMainTopicTitle[newMain], 0L,
  222.                 GetWindowDepth(theWindow), FALSE, TRUE);
  223.         }
  224.         
  225.         DisposeHandle((Handle)theMenu);
  226.     }
  227. }
  228.  
  229. void DrawTheHelpWindow(WindowPtr theWindow, short theDepth)
  230. {
  231.     GrafPtr            curPort;
  232.     short            i;
  233.     Rect            tempRect;
  234.     
  235.     curPort=(GrafPtr)theWindow;
  236.     EraseRect(&(curPort->portRect));
  237.     
  238.     tempRect=gTextRect;
  239.     InsetRect(&tempRect, -4, -4);
  240.     tempRect.right+=4;
  241.     DrawTheShadowBox(tempRect, TRUE);
  242.     
  243.     for (i=0; i<gNumMainTopics; i++)
  244.     {
  245.         Draw3DButton(&gMainTopicRect[i], gMainTopicTitle[i], 0L, theDepth, FALSE, TRUE);
  246.     }
  247.     
  248. }
  249.  
  250. /* ---------------------------------------- */
  251. /* the rest of these are internal to help.c */
  252.  
  253. static    short ParseRawTitle(Str255 theTitle)
  254. {
  255.     Str255            numStr;
  256.     long            result;
  257.     
  258.     numStr[0]=0x00;
  259.     while ((numStr[numStr[0]]=theTitle[++numStr[0]])!=' ') {}
  260.     theTitle[0]-=numStr[0];
  261.     Mymemcpy((Ptr)&theTitle[1], (Ptr)&theTitle[numStr[0]+1], theTitle[0]);
  262.     numStr[0]--;
  263.     StringToNum(numStr, &result);
  264.     
  265.     return result;
  266. }
  267.  
  268. static    void GoToPage(WindowPtr theWindow, short mainTopic, short subTopic,
  269.                         Boolean updateNow)
  270. {
  271.     StScrpHandle    styles;
  272.     TEHandle        hTE;
  273.     SignedByte        savedState;
  274.     long            textLength;
  275.     
  276.     DisposeTextResources();
  277.     GetTextResources(mainTopic, subTopic);
  278.     styles=(StScrpHandle)gTheStyle;
  279.     if (styles!=0L)
  280.         savedState=HGetState((Handle)styles);
  281.     hTE=GetWindowTE(theWindow);
  282.     if (gTheText!=0L)
  283.     {
  284.         ZapDrawHook(hTE);
  285.         HLockHi(gTheText);
  286.         textLength=GetHandleSize(gTheText);
  287.         SetTheText(theWindow, *gTheText, textLength);
  288.         HUnlock(gTheText);
  289.         if (styles!=0L)
  290.         {
  291.             TEUseStyleScrap(0, GetHandleSize(gTheText), styles, TRUE, hTE);
  292.             HSetState((Handle)styles, savedState);
  293.         }
  294.         (**hTE).destRect=(**hTE).viewRect=gTextRect;
  295.         TECalText(hTE);
  296.         RestoreDrawHook(hTE);
  297.     }
  298.     gMainTopicShowing=mainTopic;
  299.     gSubTopicShowing=subTopic;
  300.     if (updateNow)
  301.     {
  302.         InvalRect(&(theWindow->portRect));
  303.     }
  304. }
  305.  
  306. static    void GetTextResources(short mainTopic, short subTopic)
  307. {
  308.     short            resID;
  309.     
  310.     DisposeTextResources();
  311.     resID=gSubTopicID[mainTopic][subTopic];
  312.     gTheText=GetResource('TEXT', resID);
  313.     if (gTheText!=0L)
  314.         DetachResource(gTheText);
  315.     gTheStyle=GetResource('styl', resID);
  316.     if (gTheStyle!=0L)
  317.         DetachResource(gTheStyle);
  318. }
  319.  
  320. static    void DisposeTextResources(void)
  321. {
  322.     if (gTheText!=0L)
  323.         DisposeHandle(gTheText);
  324.     if (gTheStyle!=0L)
  325.         DisposeHandle(gTheStyle);
  326.     gTheText=0L;
  327.     gTheStyle=0L;
  328. }
  329.